#include <iostream>
#include <string>

using namespace std;

void* myAlloc(size_t size) {
    return malloc(size);
}

void myFree(void* ptr) {
    return free(ptr);
}

// 它们不可以被声明于一个 namespace 内
inline void* operator new(size_t size) {
    cout << "global new ()" << endl;
    return myAlloc(size);
}

inline void* operator new[](size_t size) {
    cout << "global new[]()" << endl;
    return myAlloc(size);
}

inline void operator delete(void* ptr) {
    cout << "operator delete()" << endl;
    myFree(ptr);
}

inline void operator delete[](void* ptr) {
    cout << "operator delete[]()" << endl;
    myFree(ptr);
}

class Foo {
public:
    int _id;
    long _data;
    string _str;

public:
    Foo() : _id(0) { cout << "Foo(): this = " << this << endl; }
    Foo(int i) : _id(i) { cout << "Foo(int): this = " << this << endl; }

    ~Foo() { cout << "~Foo()" << endl; }

    static void* operator new(size_t size);
    static void* operator new[](size_t size);
    static void operator delete(void* ptr);
    static void operator delete[](void* ptr);

};

void* Foo::operator new(size_t size) {
    cout << "member operator new(size_t)" << endl;
}

void* Foo::operator new[](size_t size) {
    
}

void Foo::operator delete(void* ptr) {

}

void Foo::operator delete[](void* ptr) {

}


int main(int argc, char const *argv[]) {

    Foo *pf = new Foo;
    delete pf;
    
    return 0;
}
